<template>
	<view class="document-item" @click="openDocument">
		<image v-if="info.format === 'pdf'" src="@/static/pdf.png" mode="widthFix" style="width: 60px;height: auto;"></image>
		<image v-else src="@/static/mp4.png" mode="widthFix" style="width: 60px;height: auto;"></image>
		<view class="f-s-14 text-center up-m-t-24">{{ info.name }}</view>
		<view class="f-s-10 up-info up-m-t-12">{{ info.createAt }}</view>
	</view>
</template>

<script setup>
	import Utils from '@/utils/common.js'

	const props = defineProps({
		info: {
			type: Object,
			default: () => ({})
		}
	})

	const openDocument = () => {
		const documentUrl = props.info.linkUrl || ''
		const format = props.info.format || ''
		if (!documentUrl) {
			Utils.toast('文件已丢失，请联系管理员')
			return
		}
		if(format === 'pdf'){
			uni.downloadFile({
				url: documentUrl,
				success: (res) => {
					uni.openDocument({
						filePath: res.tempFilePath,
						success: (_res) => {
							console.info('打开文档成功')
						},
						fail: (err) => {
							console.error(err)
						}
					})
				},
				fail: (err) => {
					console.error(err)
				}
			})
			return
		}
		uni.navigateTo({
			url: `/subPackages/bank/Play?videoUrl=${documentUrl}`
		})

	}
</script>

<style lang="scss" scoped>
	.document-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		background-color: #fff;
		padding: 18px 6px;
		box-sizing: border-box;
		border-radius: 4px;
	}
</style>